package com.text.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JTextField;

public class BookService {
	@SuppressWarnings("resource")
	public int Newbook(String name1, String num2, String comboBox2, String jiage3) throws SQLException, ClassNotFoundException {
		Class.forName("org.sqlite.JDBC");
		Connection conn = null;
		PreparedStatement stat = null;
		ResultSet set = null;
		try {
			conn = DriverManager.getConnection("jdbc:sqlite:frie/text.db");
			stat = conn.prepareStatement("select* from book where name=?");
			stat.setString(1, name1);
			set = stat.executeQuery();
			if (!set.next()) {
				stat = conn
						.prepareStatement("INSERT INTO book(name,price,leixing,shuliang) VALUES(?,?,?,?)");
				
				stat.setString(1, name1);
				stat.setString(2, jiage3);
				stat.setString(3, comboBox2);
				stat.setString(4, num2);
				stat.executeUpdate();
				return 1;
			} else {
				return -1;
			}
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {

					e.printStackTrace();
				}
			}
			if (stat != null) {
				try {
					stat.close();
				} catch (SQLException e) {

					e.printStackTrace();
				}
			}
			if (set != null) {
				try {
					set.close();
				} catch (SQLException e) {

					e.printStackTrace();
				}
			}
		}

	}

}
